package ru.paswd.marketprice.ui.widgets.chart

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import ru.paswd.marketprice.R
import java.util.function.Supplier

abstract class PriceHistoryView : FrameLayout {

    protected val argumentComparator = kotlin.Comparator<PriceHistoryItem> { a, b ->
        a.x.compareTo(b.x)
    }

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    abstract fun setItems(items: List<PriceHistoryItem>)

    protected fun <T : View> setWidget(widget: Supplier<T>) : T {
        val view = widget.get()
        view.layoutParams = ViewGroup.LayoutParams(
            LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        addView(view)

        return view
    }

    protected fun getPrimaryColor() = ContextCompat.getColor(context, R.color.primary)

    protected fun getGradient(): GradientDrawable {
        val primaryColor = getPrimaryColor()
        val topColor = Color.argb(127,
            Color.red(primaryColor),Color.green(primaryColor), Color.blue(primaryColor))
        val bottomColor = Color.argb(0, 0, 0, 0)
        return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
            listOf(topColor, bottomColor).toIntArray())
    }
}